<?php
/*
 * @Description:
 * @Author: QinTianyu
 * @Date: 2020-04-24 11:18:02
 * @LastEditors: QinTianyu
 * @LastEditTime: 2020-05-08 09:33:44
 */
namespace app\warehouse\model;

use think\Model;
use think\Db;
class Stock extends Model
{
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    public function detailedTypes()
    {
        $type = [
            '1' => '项目出库',
            '2' => '产品出库',
            '3' => '调拨出库',
            '4' => '出库单出库',
            '5' => '调拨入库',
            '6' => '采购入库',
            '7' => '其他出库',
            '8' => '其他入库',
            '9' => '期初入库',
        ];
        return $type;
    }
    public function getDetailedTypeTextAttr($value)
    {
        // 1=项目出库，2=产品出库，3=调拨出库，4=出库单出库，5=调拨入库，6采购入库
        $type = $this->detailedTypes();
        return $type[$value];
    }

    public function getWarehouseTextAttr($value)
    {
        $name = Db::name('warehouse')->where('id',$value)->value('name');
        return $name;
    }
    public function getUserNameAttr($value)
    {
        if($value){
            $name = Db::name('user')->where('id',$value)->value('nick');
        }else{
            $name = '--';
        }

        return $name;
    }
    /**
     * [getBeginNum 期初数量]
     * @param  [type] $warehouse_id [description]
     * @param  [type] $product_id   [description]
     * @return [type]               [description]
     */
    public function getBeginNum($warehouse_id,$product_id)
    {
        $ruNum  = $this->where('warehouse_id',$warehouse_id)->where('product_id',$product_id)->where('status',1)->where('type','入库')->sum('product_num');
        $chuNum = $this->where('warehouse_id',$warehouse_id)->where('product_id',$product_id)->where('status',1)->where('type','出库')->sum('product_num');
        return $ruNum - $chuNum;
    }
    /**
     * [getBeginPrice 期初金额]
     * @param  [type] $warehouse_id [description]
     * @param  [type] $product_id   [description]
     * @return [type]               [description]
     */
    public function getBeginPrice($warehouse_id, $product_id)
    {
        $ruPrice  = $this->where('warehouse_id',$warehouse_id)->where('product_id',$product_id)->where('status',1)->where('type','入库')->sum('price');
        $ruNum    = $this->where('warehouse_id',$warehouse_id)->where('product_id',$product_id)->where('status',1)->where('type','入库')->sum('product_num');
        if($ruNum == 0){
            return 0;
        }
        return  $ruPrice / $ruNum;
    }
    /**
     * [getEndPrice 期末金额]
     * @param  [type] $warehouse_id [description]
     * @param  [type] $product_id   [description]
     * @param  [type] $num          [产品数量]
     * @param  [type] $price        [产品单价]
     * @return [type]               [description]
     */
    public function getEndPrice($warehouse_id, $product_id, $num, $price)
    {
        $ruPrice  = $this->where('warehouse_id',$warehouse_id)->where('product_id',$product_id)->where('status',1)->where('type','入库')->sum('price');
        $ruNum    = $this->where('warehouse_id',$warehouse_id)->where('product_id',$product_id)->where('status',1)->where('type','入库')->sum('product_num');
        $ruZongNum= $ruNum + $num;
        $ruZongPrice = $price * $num + $ruPrice;

        return  $ruZongPrice / $ruZongNum;
    }
    /**
     * 获取产品入库单价 function
     *
     * @param [type] $warehouse_id
     * @param [type] $product_id
     * @return void
     */
    public function getProductPrice($warehouse_id,$product_id)
    {
        // 入库金额
        $ruPrice  = $this->where('warehouse_id',$warehouse_id)->where('product_id',$product_id)->where('status',1)->where('type','入库')->sum('price');
        // 入库数量
        $ruNum    = $this->where('warehouse_id',$warehouse_id)->where('product_id',$product_id)->where('status',1)->where('type','入库')->sum('product_num');
        // 查询调拨出库的数据并且排除
        $chuPrice  = $this->where('warehouse_id',$warehouse_id)->where('product_id',$product_id)->where('status',1)->where('type','出库')->where('detailed_type',3)->sum('price');
        $chuNum    = $this->where('warehouse_id',$warehouse_id)->where('product_id',$product_id)->where('status',1)->where('type','出库')->where('detailed_type',3)->sum('product_num');
        if($ruNum == 0 || $ruNum - $chuNum == 0){
            return 0;
        }
        return  round(($ruPrice - $chuPrice) / ($ruNum - $chuNum),2);
    }

}
